<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/md5.js"></script>
</head>

<body>
    <style>
        html,
        body {
            height: 100%;
        }

        .aui-list {
            background: none;
        }

        .aui-list li {
            background: #fff;
        }
    </style>
    <div id="cover">
        <div id="cover_left"></div>
        <div id="cover_right"></div>
        <div id="cover_foot"></div>
        <div id="cover_top"></div>
        <div class="login-t-1">市管领导干部信息查询系统</div>
        <div class="login-t-2">中共佳木斯市委组织部</div>
    </div>
    <!-- 登录 -->
    <div class="aui-content login">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-my"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="账号" name='name' value="" id='login'>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-lock"></i>
                    </div>
                    <div class="aui-list-item-input">
                        <input type="password" placeholder="密码" name='password' value="" id='password'>
                    </div>
                    <!-- <div class="aui-list-item-label-icon">
                    <i class="aui-iconfont aui-icon-display"></i>
                </div> -->
                </div>
            </li>

        </ul>
        <div class="aui-list-item-inner login-radio">
            <div class="aui-list-item-input">
                <label><input class="aui-radio" type="checkbox" name="rember" checked='checked' >记住密码</label>
            </div>
        </div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="login()">登录</div>

    </div>
</body>
<script type="text/javascript" src="./script/api.js"></script>

</html>

<script>
    function login() {
    var rember = $("input[type='checkbox']").is(':checked');
    $api.setStorage('rember', rember);

        var userData = $api.getStorage('userData');
        var loginName = $('input[name="name"]').val();
        var password = $('input[name="password"]').val();
        loginName = loginName.trim();
        loginName = loginName.toLowerCase();
        password = password.toUpperCase();
        // passwordMd5 = $.md5(password);
        // console.log(password);
        for (var i = 0; i < userData.length; i++) {
            userData[i].LoginName = userData[i].LoginName.toLowerCase();
            if (userData[i].LoginName == loginName) {
              if (userData[i].Password == password) {
                $api.setStorage('currentUser', loginName);
                $api.setStorage('currentPassWord', password);
                api.openWin({
                    name: 'index',
                    url: './index.html',
                });
                return;
            }else if(userData[i].Password == $.md5(password).toUpperCase()){
              $api.setStorage('currentUser', loginName);
              $api.setStorage('currentPassWord', $.md5(password).toUpperCase());
              api.openWin({
                  name: 'index',
                  url: './index.html',
              });
              return;
            }
              }
        }
        api.toast({
            msg: '用户名或密码错误',
            duration: 2000,
            location: 'bottom'
        });
    }

    apiready = function() {
        is_newData();
        api.setScreenOrientation({
            orientation: 'auto'
        });

        //index 刷新用
        $api.setStorage('tabIndex', null);
        api.addEventListener({
                name: 'keyback'
            }, function(ret, err) {
                api.closeWidget();
            })
            is_wifi();
    }

    function is_newData() {
    var login = $api.getStorage('currentUser');
    var pass = $api.getStorage('currentPassWord');
    var rember = $api.getStorage('rember');
    // rember='1';
    if (login !='' && pass !='' && rember !='false') {
      $('#login').val(login);
      $('#password').val(pass);
    }
        var user = $api.getStorage('userData');
        if (user) {
            return;
        }
        var sql = "SELECT * FROM SYsuserList WHERE ugrade !='管理' ";
        var db = api.require('CPDB');
        db.selectSql({
            name: 'data',
            sql: sql
        }, function(ret, err) {
            if (ret.status) {

                $api.setStorage('userData', ret.data);
                // console.log(ret.data);
            } else {
                // alert(JSON.stringify(err));
            }
        });
    }

    function is_wifi() {
        var wifissid = api.require('wifiSSID');
        wifissid.getSsid(null, function(ret, err) {
            if (ret.ssid && ret.ssid != '0x') {
                alert('请关闭wifi!');
                api.closeWidget({
                    id: 'A6076154446731',
                    retData: {
                        name: 'closeWidget'
                    },
                    silent: true,
                    animation: {
                        type: 'flip',
                        subType: 'from_bottom',
                        duration: 500
                    }
                });
            }
        });


        api.addEventListener({
            name: 'online'
        }, function(ret, err) {
            alert('请关闭wifi!');
            api.closeWidget({
                id: 'A6076154446731',
                retData: {
                    name: 'closeWidget'
                },
                silent: true,
                animation: {
                    type: 'flip',
                    subType: 'from_bottom',
                    duration: 500
                }
            });
        });
    }
</script>
